package gui.data.others;

import gui.ErrorMessage;
import gui.MyFrame;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.TextField;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class SingleInputGui extends JPanel {

	String tekst = "", title = "";
	int guide;
	TextField input;

	public SingleInputGui(final int guide, final MyFrame myFrame) {
		this.guide = guide;
		switch (guide) {
		case 1:
			tekst = "Insert number of digits to calculate (Max 1000000)";
			title = "Nth digit of PI";
			break;
		case 2:
			tekst = "How far to calculate Fibonacci sequence (Max 10000)";
			title = "Fibonacci sequence";
			break;
		case 3:
			tekst = "To what number from 0 to get all Prime numbers (Max 1000000)";
			title = "Prime numbers";
			break;
		}
		this.setLayout(new GridLayout(3, 1));
		this.setPreferredSize(new Dimension(500, 500));
		JLabel lbl = new JLabel(tekst, JLabel.CENTER);
		input = new TextField();
		this.add(lbl);
		this.add(input);
		JButton calculate = new JButton("Calculate");
		calculate.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				String inputTxt = input.getText();
				switch (guide) {
				case 1:
					if (inputTxt == null || inputTxt.equals("") || !isInteger(inputTxt) || Integer.parseInt(inputTxt) > 1000000 || Integer.parseInt(inputTxt) < 0){
						new ErrorMessage("Insert valid number (Between 0 and 1000000)");
					} else {
						myFrame.setDataPanel(new SingleOutputGui(guide, inputTxt), "DATA_OUTPUT_PI");
					}
					break;
				case 2:
					if (inputTxt == null || inputTxt.equals("") || !isInteger(inputTxt) || Integer.parseInt(inputTxt) > 10000 || Integer.parseInt(inputTxt) < 1){
						new ErrorMessage("Insert valid number (Between 1 and 10000)");
					} else {
						myFrame.setDataPanel(new SingleOutputGui(guide, inputTxt), "DATA_OUTPUT_FIBO");
					}
					break;
				case 3:
					if (inputTxt == null || inputTxt.equals("") || !isInteger(inputTxt) || Integer.parseInt(inputTxt) > 1000000 || Integer.parseInt(inputTxt) < 2){
						new ErrorMessage("Insert valid number (Between 2 and 1000000)");
					} else {
						myFrame.setDataPanel(new SingleOutputGui(guide, inputTxt), "DATA_OUTPUT_PRIME");
					}
					break;
				}
			}
		});
		this.add(calculate);
	}

	public boolean isInteger(String input) {
		try {
			Integer.parseInt(input);
			return true;
		} catch (Exception e) {
			return false;
		}
	}
}
